# -*- coding = utf-8 -*-
# @Time : 2022/3/11 9:37
# @Author :Henry L
# @File : nash.py
# @Software : PyCharm
# @change 更改了subprocess.py中的 encoding,设置为了 utf-8
import csv
import json
import math
import random
import time
import execjs
import requests
import logging
from sqlalchemy import create_engine
import pandas as pd


logging.basicConfig(level=logging.DEBUG,filename='xiangmu.log', format='%(asctime)s - %(name)s - %(levelname)s - %(message)s') #设置日志级别
"""注意这里我改了execjs中文件的编码格式"""
regn_code_url = "https://fuwu.nhsa.gov.cn/ebus/fuwu/api/nthl/api/dic/queryAdmdvsTree"
lv_and_type_url = "https://fuwu.nhsa.gov.cn/ebus/fuwu/api/nthl/api/fixed/queryDicByType"
# result_url = "https://fuwu.nhsa.gov.cn/ebus/fuwu/api/nthl/api/fixed/queryFixedHospital"
result_url = "https://fuwu.nhsa.gov.cn/ebus/fuwu/api/nthl/api/CommQuery/queryFixedHospital"
UA = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.51 Safari/537.36"

with open('nash.js', 'r', encoding='utf-8') as f:
    nhsa_js = execjs.compile(f.read())


def get_headers():
    """获取 header 参数，每次请求改变"""
    headers = nhsa_js.call("getHeaders")
    headers["User-Agent"] = UA
    headers["Content-Type"] = "application/json"
    headers["Host"] = "fuwu.nhsa.gov.cn"
    headers["Origin"] = "https://fuwu.nhsa.gov.cn"
    headers["Referer"] = "https://fuwu.nhsa.gov.cn/nationalHallSt/"
    print(headers)
    return headers


def get_regn_code():
    """获取城市代码，返回结果无加密"""
    payload = {"data": {"transferFlag": ""}}
    response = requests.post(url=regn_code_url, json=payload, headers=get_headers())
    region_dic = json.loads(response.text)
    return region_dic['data']



def get_medins_lv_or_type_code(key):
    """获取医疗机构等级 (LV) or 类型 (TYPE) 代码"""
    if key == "LV":
        payload = {"type": "MEDINSLV"}
    elif key == "TYPE":
        payload = {"type": "MEDINS_TYPE"}
    else:
        print("输入有误!")
        return
    encrypted_payload = nhsa_js.call("getEncryptedData", payload)
    encrypted_data = requests.post(url=lv_and_type_url, json=encrypted_payload, headers=get_headers()).json()
    decrypted_data = nhsa_js.call("getDecryptedData", encrypted_data)
    return decrypted_data

def get_result(region,pagenum):
    s = requests.session()
    s.keep_alive = False
    addr = ""
    openElec = ""
    medins_lv_code = ""
    medins_name = ""
    medins_type_code = ""
    regn_code = region

    payload = {
        "addr": addr,
        "medinsLvCode": medins_lv_code,
        "medinsName": medins_name,
        "medinsTypeCode": medins_type_code,
        "openElec": openElec,
        "pageNum": pagenum,
        "pageSize": 100,
        "regnCode": regn_code,
        }
    encrypted_payload = nhsa_js.call("getEncryptedData", payload)
    sleep_time = 0.1 * random.randint(1, 20)
    time.sleep(random.randint(10, 50) + sleep_time)
    encrypted_data = requests.post(url=result_url, json=encrypted_payload, headers=get_headers(), timeout=60).json()
    while encrypted_data['code'] != 0:
        time.sleep(random.randint(10, 20) + sleep_time)
        encrypted_data = requests.post(url=result_url, json=encrypted_payload, headers=get_headers(), timeout=60).json()
    decrypted_data = nhsa_js.call("getDecryptedData", encrypted_data)
    return decrypted_data


def to_csv(result, writer, province, city):
    for row in result:
        row["province"] = province
        if city != '':
            row["city"] = city
        writer.writerow(row)


def main():
    with open('data1.csv', 'a+', encoding='utf-8', newline='') as file:
        header = ['medinsTypeName','medinsNatu','hospLv','medinsLvName','medinsType','medinsLv',
                  'medinsName','addr','medinsCode','medinslvCode','medinsNatuName','province','city','openElec','lnt','lat']
        lv_dict = dict
        type_dict = dict
        wirter = csv.writer(file)
        wirter.writerow(header)
        # 获取城市代码
        w = csv.DictWriter(file, header)
        region_dic = get_regn_code()
        num = 1
        for region in region_dic:
                if num > 32:
                    break
                # if num < 16:
                #     num = num + 1
                #     continue
                if region['name'] == '北京市' or region['name'] == '天津市' or\
                        region['name'] == '重庆市' or region['name'] == '新疆生产建设兵团':
                    region_code = region['code']
                    result = get_result(region_code, 1)
                    total_num = result['total']
                    data_ls = result['list']
                    to_csv(data_ls, w, region['name'], region['name'])
                    max_page = math.ceil(total_num / 100)
                    # 默认max_page 是大于2的
                    time.sleep(5)
                    for page in range(2, max_page+1):
                        try:
                            result_n = get_result(region_code, page)
                        except Exception as e:
                            # logging.info('the exception error is %s, other is %s' % (e.args, repr(e)))
                            time.sleep(90)
                            continue
                        data_ls_n = result_n['list']
                        to_csv(data_ls_n, w, region['name'], region['name'])
                else:
                    province = region['name']
                    city_ls = region['list']
                    for city in city_ls:
                        try:
                            result = get_result(city['code'], 1)
                        except:
                            time.sleep(120)
                            result = get_result(city['code'], 1)
                        finally:
                            total_num = result['total']
                            data_ls = result['list']
                            to_csv(data_ls, w, province, city['name'])
                            if total_num > 100:
                                max_page = math.ceil(total_num / 100)
                                for page in range(2, max_page + 1):
                                    try:
                                        result_n = get_result(city['code'], page)
                                    except Exception as e:
                                        # logging.info('the exception error is %s, other is %s and occur at %s pagenum is %s'  % (e.args, repr(e), city, page))
                                        time.sleep(120)
                                        continue
                                    data_ls_n = result_n['list']
                                    to_csv(data_ls_n, w, province, city['name'])

                        time.sleep(10)
                num = num + 1
                time.sleep(100)
    file.close()
    # 获取地区编码
    # region_dic = get_regn_code()
    # 获取医疗机构等级代码
    # level_ls = get_medins_lv_or_type_code("LV")
    # print(level_ls)
    # for i in level_ls:
    #     lv_dict.update({i['value']: i['text']})
    #     print(lv_dict['value'])
    # # 获取医疗机构类型代码
    # type_ls = get_medins_lv_or_type_code("TYPE")
    # print(type_ls)
    # 获取搜索结果







if __name__ == "__main__":
    main()
    # time.sleep(10)
    # dff = pd.read_csv('data1.csv')
#     decrypted_data = nhsa_js.call("getDecryptedData", {
#   "code": 0,
#   "data": {
#     "signData": "eZnTfWA3ShWQQhU5cnzbgb0w+EZtlW+etQlqBL2U+fi0ByTtGVQfBOz9wNxTqkHSm2hpcYbauR6qC6YT1gZfYg==",
#     "encType": "SM4",
#     "data": {
#       "encData": "92194CC73622AD6CF973564915AA63E2BAD79013BC1A4B714E3F6F4F8BD97DE9F2019F242D95A1454102342C53DE14165F6252CD06C9712B0546616145170082F65719A7D013B1DF763B2382C2EB594C"
#     },
#     "signType": "SM2",
#     "appCode": "T98HPCGN5ZVVQBS8LZQNOAEXVI9GYHKQ",
#     "version": "1.0.0",
#     "timestamp": "1717059149045"
#   },
#   "message": "成功",
#   "timestamp": "1717059149",
#   "type": "success"
# })
#     print(decrypted_data)
    # engine = create_engine('postgresql://postgres:j3IA6rebQ9NG@192.168.6.226:5432/odp20210825')
    # '''index : bool, default True
    #             Write DataFrame index as a column. Uses `index_label` as the column
    #             name in the table.'''
    # dff.to_sql('yibao_accountbase_' + time.strftime('%Y%m%d', time.localtime(time.time())),
    #            engine, index=False, if_exists='replace')
